#!/usr/bin/python
# -*- coding: utf-8 -*-

# cd /d D:\python2.6.6\ 
#  python.exe  E:\py_dev\test2.py


"""
图形 开发 支持  utf-8
"""

# Copyright (c) 2001-2004 Twisted Matrix Laboratories.  linux 就是 deb： python-wxtools包  rpm ：wxPython-devel.x86_64
# See LICENSE for details.  wxPython2.8-win32-unicode-2.8.11.0-py26.exe
# cd /d D:\python2.6.6\dev_test 
# ..\python.exe wxp.py
# GUI Design Studio 界面设计工具

# EasyGuider 

#  wxGlade  wxpy ide   #一般
#  WPDB” (使用WinPdb作为调试器)  #没用过
# PythonWin                  #没用过
# Ulipad 国产                 #没用过
# Boa Constructor ide       #没用过         +++++++ 很好用


# cd  /d D:\python2.6.6
# python D:\ss\wxglade.py


#http://www.hackchina.com/dlpre.php?id=100307   代码搜索



import os,time,sys

import wx
ID_OPEN = 101
ID_EXIT = 110
ID_SAVE = 111
ID_BUTTON = 112

class MainWindow(wx.Frame):
    """ We simply derive a new class of Frame. """
    def __init__(self ):
        pass
        #self.xinit(None,-1, 'Small editor')
            
    def xinit(self, parent, id, title):
        
        
        wx.Frame.__init__(self, parent, id, title, size=(800,500))
        self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
        self.CreateStatusBar()
        filemenu  = wx.Menu()
        filemenu.Append(ID_OPEN,u"打开文件","open file")
        filemenu.AppendSeparator()
        filemenu.Append(ID_SAVE,u"保存文件"," save file")
        filemenu.AppendSeparator()
        filemenu.Append(ID_EXIT,u"退出","exit")
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,u"菜单")
        self.SetMenuBar(menuBar)
        #wx.EVT_MENU(self,ID_OPEN,self.open)
        self.Bind(wx.EVT_MENU, self.open, id=ID_OPEN)  #绑定事件的模式

        wx.EVT_MENU(self,ID_EXIT,self.exit)
        wx.EVT_MENU(self,ID_SAVE,self.save)
        self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        self.buttons = []
        
#        for i in range(0,6):
#            self.buttons.append(wx.Button(self,ID_BUTTON+i,"Button &"+'i'))    
#            self.sizer2.Add(self.buttons[i],1,wx.EXPAND)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.control,1,wx.EXPAND)
        self.sizer.Add(self.sizer2,0,wx.EXPAND)
        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.sizer.Fit(self)
        
        self.Show(True)
        

    def exit(self,e):
        '''用户退出窗口'''
        self.Close(True)
        
    def open(self,e):
        '''打开文件 '''
        self.dirname = ''
        dlg = wx.FileDialog(self,"chose a file",self.dirname,"","*.*",wx.OPEN)
        
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
            f = open(os.path.join(self.dirname,self.filename),'r')
            self.control.SetValue(f.read())
            f.close()
        dlg.Destroy()
        
    def save(self,e):
        '''保存文件'''
        try:
            f = open(os.path.join(self.dirname,self.filename),'w')
        except AttributeError:
            print '文件不存在'
            sys.exit(0)
            
        content = self.control.GetValue()
        try:
            f.write(content)
        except UnboundLocalError:
            print '文件不存在'
            sys.exit(0)
        finally:
            f.close()
            
            
    def test(self):
        
        
        frame=self.xinit(None,-1, 'Small editor')
        

def test():

    #app = wx.PySimpleApp()
    framex=MainWindow( )
    framex.test()
    #framex=MainWindow(None,-1, '--Small editor--')
    #app.MainLoop()
    
    
if __name__ == '__main__':
    #time.sleep(10)
    obj=MainWindow()
    obj.test()

    #test()
    